<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box {
            position: absolute;
            width: 100px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div id="box"></div>

    <script>
        const box = document.getElementById('box');
        let ml = 10;
        let dml = 10;
        setInterval(() => {
            if(ml > document.body.clientWidth - 100 || ml < 10)
        {
            dml = -dml;
        }
        ml = ml + dml;
        stringml = ml + 'px';
        box.style.marginLeft = stringml;
        console.log(stringml);
        },100);
        if(ml > 100){
            ml=40;
        }
    </script>
</body>
</html>